package util;

import java.awt.Color;
import java.io.Serializable;

public class Color3f extends Tuple3f implements Serializable
{
    /**
	 * 
	 */
	private static final long serialVersionUID = -6086106238252826991L;


	/**
     * Constructs and initializes a Color3f to (0.0, 0.0, 0.0).
     */

    public Color3f()
    {
    }


    /**
     * Constructs and initializes a Color3f from the three xyz values.
     * @param r
     * @param g
     * @param b
     */

    public Color3f(float r, float g, float b)
    {
        super(r, g, b);
    }


    /**
     * Constructs and initializes a Color3f from the specified Color3f. 
     * @param c
     */

    public Color3f(Color3f c)
    {
        super(c);
    }


    /**
     * Constructs and initializes a Color3f from the specified Tuple3f.
     * @param t
     */

    public Color3f(Tuple3f t)
    {
        super(t);
    }


    /**
     * Constructs and initializes a Color3f from the array of length 3.
     * @param c
     */

    public Color3f(float c[])
    {
        super(c);
    }


    /**
     * Constructs and initializes a Color3f from the specified AWT Color object.
     * @param c
     */

    public Color3f(Color c)
    {
        super((float)c.getRed() / 255F, (float)c.getGreen() / 255F, (float)c.getBlue() / 255F);
    }


    public Color3f(double r, double g, double b) {
		this((float)r,(float)g,(float)b);
	}


	/**
     * Sets the r,g,b values of this Color3f object to those of the specified AWT Color object.
     * @param c
     */

    public final void set(Color c)
    {
        x = (float)c.getRed() / 255F;
        y = (float)c.getGreen() / 255F;
        z = (float)c.getBlue() / 255F;
    }


    /**
     * Returns a new AWT color object initialized with the r,g,b values of this Color3f object.
     * @return
     */

    public final Color get()
    {
        int i = Math.round(x * 255F);
        int j = Math.round(y * 255F);
        int k = Math.round(z * 255F);
        return new Color(i, j, k);
    }
    
    public Color3f add(Color3f c){
    	Color3f newColor = new Color3f(this);
    	newColor.setR(this.x + c.x);
    	newColor.setG(this.y + c.y);
    	newColor.setB(this.z + c.z);
    	return newColor;
    }
    
    public Color3f scale(float scale){
    	Color3f newColor = new Color3f(this);
    	newColor.setR(this.x * scale);
    	newColor.setG(this.y * scale);
    	newColor.setB(this.z * scale);
    	return newColor;
    }
    
    public Color3f multiply(Color3f mult){
    	return (new Color3f(this.x * mult.x, this.y * mult.y, this.z * mult.z));
    }
    
    public void setR(float r){
    	if(r < 0)
    		this.x = 0;
    	else if(r > 1)
    		this.x = 1;
    	else
    		this.x = r;
    }
    
    public void setG(float g){
    	if(g < 0)
    		this.y = 0;
    	else if(g > 1)
    		this.y = 1;
    	else
    		this.y = g;
    }
    
    public void setB(float b){
    	if(b < 0)
    		this.z = 0;
    	else if(b > 1)
    		this.z = 1;
    	else
    		this.z = b;
    }
    
    public Vector3f toVector3f(){
    	return new Vector3f(x,y,z);
    }
}
